package sadi.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


public class FileHandler {

	private String filename;
	private File inputFile;
	private boolean status;
	
	public FileHandler(String name)
	{
		filename = name;
	}
	
	public String readFile() 
	{
		String data = "", buffer = "";
		FileReader file;
		try {
			file = new FileReader(filename);
		} catch (FileNotFoundException e) {
			return null;	
		}
		BufferedReader in = new BufferedReader(file);
		try {
			buffer = in.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		while(buffer != null)
		{
			data += buffer;
			try {
				buffer = in.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;
	}
	
	public void writeFile(String outputData)
	{
		FileWriter file = null;
		
		try {
			file = new FileWriter(filename, false);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PrintWriter out = new PrintWriter(new BufferedWriter(file));
		out.write(outputData);	
		out.close();
	}
}

